import React, { Component } from 'react';
import BreadcrumbCustom from '../common/BreadcrumbCustom';
import { message, Table } from 'antd';
import fetchRequest from '../../fetch/fetchRequest';

export default class Order extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource: [],
            loading: true
        };
    }
    // 获取订单列表
    getUsersList() {
        fetchRequest.fetchR('orders', 'GET')
        .then((response) => {
            response.sort(function (a, b) {
                return Date.parse(b.orderTime) - Date.parse(a.orderTime); //时间降序
            })

            let dataList = []
            let dayCurrent
            if (response.length !== 0) {
                dayCurrent = response[0].orderTime.split(' ')[0]
            }
            let dayPrincipal = 0
            let dayCommission = 0
            let dayNumber = 0
            response.forEach((item, index) => {
                if (item.orderTime.split(' ')[0] === dayCurrent) {
                    dayPrincipal += item.orderPrincipal
                    dayCommission += item.orderCommission
                    dayNumber += 1
                    if (response.length === (index + 1)) { // 最后一天的数据
                        const newData = {
                            orderTime: dayCurrent,
                            orderNumber: dayNumber,
                            orderPrincipal: dayPrincipal.toFixed(2),
                            orderCommission: dayCommission.toFixed(2) 
                        }
                        dataList.push(newData)
                    }
                } else {
                    const newData = {
                        orderTime: dayCurrent,
                        orderNumber: dayNumber,
                        orderPrincipal: dayPrincipal.toFixed(2),
                        orderCommission: dayCommission.toFixed(2) 
                    }
                    dataList.push(newData)
                    dayCurrent = item.orderTime.split(' ')[0]
                    dayCommission = item.orderCommission
                    dayPrincipal = item.orderPrincipal
                    dayNumber = 1
                }
            })

            dataList.map((item, index) => item.key = index)

            this.setState({
                dataSource: dataList,
                loading: false
            })
        }).catch((err) => {  //网络请求失败返回的数据
            message.error('数据获取失败')
        })
        // fetchRequest.fetchR('ordersStatistics', 'GET')
        // .then((response) => {
        //     console.log(response)
        // }).catch((err) => {  //网络请求失败返回的数据
        //     message.error('数据获取失败')
        // })
    }

    // 渲染
    componentDidMount() {
        this.getUsersList();
    }

    render() {
        const { dataSource, loading } = this.state

        const columns = [{
            title: '时间',
            dataIndex: 'orderTime'
        }, {
            title: '订单数',
            dataIndex: 'orderNumber',
            sorter: (a, b) => a.orderNumber - b.orderNumber
        }, {
            title: `本金(元)`,
            dataIndex: 'orderPrincipal',
            sorter: (a, b) => a.orderPrincipal - b.orderPrincipal
        }, {
            title: `佣金(元)`,
            dataIndex: 'orderCommission',
            sorter: (a, b) => a.orderCommission - b.orderCommission
        }]

        return (
            <div>
                <BreadcrumbCustom paths={['订单管理']} />
                <div className='formBody'>
                    <Table
                        columns={columns}
                        dataSource={dataSource}
                        bordered={true}
                        className='formTable'
                        loading={loading}
                        pagination={{ defaultPageSize: 20 }}
                    />
                </div>
            </div>
        )
    }
}